<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" title="履职管理"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="姓名" v-if="tabsCurrent == 0"><u-input v-model="perform.userName" type="text" placeholder="请输入姓名" /></u-form-item>
					<u-form-item class="form-item" label="组别" v-else><u-input v-model="perform.wyhdzName" type="text" placeholder="请输入组别" /></u-form-item>
					<!-- <u-form-item class="form-item" label="届次">
						<u-input v-model="jieci" type="select" :select-open="jcShow" placeholder="请选择届次" @click="jcShow = true" />
						<u-select v-model="jcShow" mode="mutil-column-auto" :list="jcList" @confirm="jcConfirm"></u-select>
					</u-form-item> -->
					<!-- <u-form-item class="form-item" label="年度"><date-time-picker placeholder="请选择年度" :defaultValue="perform.year" fields="year" :old="true" @change="yearChange"></date-time-picker></u-form-item> -->
					<u-form-item class="form-item" label="开始时间">
						<date-time-picker placeholder="请选择开始时间" :defaultValue="startTime" :old="true" fields="day" @change="timeChange($event, 'startTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" label="结束时间">
						<date-time-picker placeholder="请选择结束时间" :defaultValue="endTime" :old="true" fields="day" @change="timeChange($event, 'endTime')"></date-time-picker>
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>

		<!-- <u-tabs class="uTabs" :list="tabsList" :is-scroll="false" bgColor="rgb(229, 35, 26)" inactiveColor="#fff" activeColor="#fff" :line="true" barWidth="114" :current="tabsCurrent" @change="tabsChange"></u-tabs> -->

		<view class="tab-nav">
			<!-- <view class="tab-item" :class="tabIndex == 0 ? 'active' : ''" @click="tab(0)">本年度</view>
			<view class="tab-item" :class="tabIndex == 1 ? 'active' : ''" @click="tab(1)">本届</view> -->
			<view class="tab-text" @click="jcShow = true">
				{{ jieci }}
				<u-icon class="right-icon" :class="{ reverse: jcShow }" name="arrow-down-fill" size="26" color="#c0c4cc"></u-icon>
			</view>
			<u-select v-model="jcShow" mode="mutil-column-auto" :list="jcList" @confirm="jcConfirm"></u-select>

			<view class="search-filter" @click="show = !show"><view>筛选</view></view>
		</view>
		<view class="table-box" v-show="tabsCurrent == 0">
			<u-table class="table-inner" border-color="#F8F8F8" key="tabsCurrent">
				<u-tr>
					<u-th style="flex: 0 0 300rpx">代表姓名</u-th>
					<u-th>履职得分</u-th>
					<u-th style="flex: 0 0 140rpx">排名</u-th>
				</u-tr>
				<view class="table-cell" v-for="(item, index) of dutiesList" :key="index" @click="navTo(item)">
					<u-tr>
						<u-td style="flex: 0 0 300rpx">
							<view class="name">
								<image :src="$imagePreview(item.photo)" mode="aspectFill"></image>
								<text class="clamp">{{ item.userName }}</text>
							</view>
						</u-td>
						<u-td>{{ item.sumScore }}</u-td>
						<u-td style="flex: 0 0 140rpx">{{ item.ranking }}</u-td>
					</u-tr>
				</view>
			</u-table>

			<u-loadmore :status="loading" />
		</view>
		<view class="table-box" v-show="tabsCurrent == 1">
			<u-table class="table-inner" border-color="#F8F8F8" key="tabsCurrent">
				<u-tr>
					<u-th style="flex: 0 0 300rpx">组别</u-th>
					<u-th>履职得分</u-th>
					<u-th style="flex: 0 0 140rpx">排名</u-th>
				</u-tr>
				<view class="table-cell" v-for="(item, index) of dutiesList" :key="index" @click="navToGroup(item)">
					<u-tr>
						<u-td style="flex: 0 0 300rpx">
							<text class="clamp">{{ item.wyhdzName }}</text>
						</u-td>
						<u-td>{{ item.sumScore }}</u-td>
						<u-td style="flex: 0 0 140rpx">{{ item.ranking }}</u-td>
					</u-tr>
				</view>
			</u-table>

			<u-loadmore :status="loading" />
		</view>
		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabIndex: 0,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '代表履职',
					value: 0
				},
				{
					name: '活动组履职',
					value: 1
				}
			],
			jieci: '',
			jcShow: false,
			jcList: [],
			dutiesList: [],

			startTime: '',
			endTime: '',

			perform: {
				year: '',
				ciId: '',
				jieId: '',
				timeScope: '',
				assessLevel: '',
				userName: '',
				wyhdzName: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadData();
		this.jieciData();
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		timeScope: {
			get: function () {
				let time = this.startTime + ' - ' + this.endTime;
				if (this.startTime == '' || this.endTime == '') {
					return '';
				} else {
					return time;
				}
			},
			set: function (e) {
				this.startTime = '';
				this.endTime = '';
			}
		},
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
				this.roleCodes = this.userInfo.roleCodes.join(',');
				this.jieci = this.userInfo.jieCi.pName + this.userInfo.jieCi.name;

				// this.perform.year = this.userInfo.jieCi.year;
				this.perform.jieId = '';

				this.pageNum = 1;
				this.pageSize = 15;
			} else {
				this.$logout();
			}
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				currentUserId: this.userId,
				clientCode: this.clientCode,
				roleCodes: this.roleCodes,
				...this.perform
			};
			let url = '/perform/score/eval_wyhdz';
			if (this.tabsCurrent == 0) {
				url = '/perform/score/eval1';
			}
			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.dutiesList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.dutiesList = this.dutiesList.concat(res.data.records);
				this.loaded = true;
				if (this.dutiesList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		tab(e) {
			this.tabIndex = e;
			if (e == 0) {
				this.perform.year = this.userInfo.jieCi.year;
				this.perform.jieId = '';
			} else {
				this.perform.year = '';
				this.perform.jieId = this.userInfo.jieCi.pid;
			}
			this.loadData('refresh');
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.resert();
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		yearChange({ date }) {
			this.perform.year = date.year.toString();
		},
		jcConfirm(e) {
			this.jieci = e[0].label + e[1].label;
			this.perform.jieId = e[0].value;
			this.perform.ciId = e[1].value;

			this.dutiesList = [];
			this.loadData('refresh');
		},
		filter() {
			if (this.startTime == '' && this.endTime != '') {
				this.$msg('请选择开始时间');
				return false;
			}
			if (this.startTime != '' && this.endTime == '') {
				this.$msg('请选择结束时间');
				return false;
			}
			let arr = this.startTime.split('-');
			let startTime = new Date(arr[0], arr[1], arr[2]).getTime();

			let arrs = this.endTime.split('-');
			let endTime = new Date(arrs[0], arrs[1], arrs[2]).getTime();

			if (startTime >= endTime) {
				this.$msg('开始时间应小于结束时间');
				return false;
			}

			this.show = false;
			this.perform.timeScope = this.timeScope;
			this.tabIndex = 0;
			this.dutiesList = [];
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.timeScope = '';
			this.perform.timeScope = '';
			this.perform.userName = '';
			this.perform.wyhdzName = '';
			this.jieci = '';
			this.perform.jieId = '';
			this.perform.ciId = '';
			this.tabIndex = 0;
			this.dutiesList = [];
			this.loadData('refresh');
		},
		search(e) {
			this.perform.userName = e;
			this.loadData('refresh');
		},
		jieciData() {
			let data = {
				clientCode: this.clientCode,
				jieCi: {
					pid: ''
				}
			};
			this.$request('/jie-ci/list', data).then((res) => {
				this.jcList = [];
				let jieArr = [];
				let ciArr = [];
				res.data.forEach((item) => {
					data.jieCi.pid = item.id;
					this.$request('/jie-ci/list', data).then((res) => {
						ciArr = [];
						res.data.forEach((key) => {
							ciArr.push({
								value: key.id,
								label: key.name
							});
						});
						this.jcList.push({
							value: item.id,
							label: item.name,
							children: ciArr
						});
					});
				});
			});
		},
		navTo(item) {
			uni.navigateTo({
				url: '/pagesA/duties/self?currentUserId=' + item.userId
			});
		},
		navToGroup(item) {
			uni.navigateTo({
				url: '/pagesA/duties/group?wyhdz=' + item.wyhdz
			});
		}
	}
};
</script>

<style lang="scss">
.tab-nav {
	position: relative;
	font-size: 14px;
	color: #666;
	line-height: 60rpx;
	text-align: center;
	background-color: #fff;

	.tab-text {
		margin: 18rpx 10rpx;
		height: 60rpx;
		font-size: 15px;
		font-weight: 500;
		color: #333;
		line-height: 60rpx;
	}

	.tab-item {
		margin: 18rpx 10rpx;
		position: relative;
		width: 140rpx;
		height: 60rpx;
		color: #666;
		border-radius: 6rpx;
		background-color: #e6e7e8;
	}

	.tab-item.active {
		font-weight: bold;
		color: #fff;
		background-color: #e5231a;
	}

	.search-filter {
		position: absolute;
		right: 30rpx;
		top: 18rpx;
		width: 120rpx;
		height: 60rpx;
		border-radius: 6rpx;
		background-color: #e6e7e8;
	}

	.right-icon {
		margin-left: 10rpx;
		transition: transform 0.4s;

		&.reverse {
			transform: rotate(-180deg);
		}
	}
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.table-box {
	.name {
		display: flex;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: center;

		text {
			color: #e5231a;
			line-height: 80rpx;
			width: 160rpx;
			text-align: left;
		}
	}

	text {
		color: #e5231a;
	}

	image {
		margin-right: 20rpx;
		width: 80rpx;
		height: 80rpx;
		border-radius: 80rpx;
	}
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid $u-border-color;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
